package com.vegan_friendly;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.Toast;

public class MapRestActivity extends MapActivity {
    private MapView mapView;
    GeoPoint p;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		final MapView mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		
		Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
		final MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(drawable, this);
		
		// Acquire a reference to the system Location Manager
		LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

		// Define a listener that responds to location updates
		LocationListener locationListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {
		      // Called when a new location is found by the network location provider.
				MapController mapController = mapView.getController();
				double lat = location.getLatitude();
				double lon = location.getLongitude();
				GeoPoint point = new GeoPoint((int)(lat * 1E6),(int)(lon * 1E6));
				mapController.setZoom(18);
		        mapController.animateTo(point);
		        
		        OverlayItem item = new OverlayItem(point, "", "");
		   
		        List< Overlay > mapOverlays = mapView.getOverlays();
		        mapOverlays.clear();
		        itemizedoverlay.addOverlay(item);
		        mapOverlays.add(itemizedoverlay);
		        
		      /*  MapOverlay mapOverlay = new MapOverlay();
		        List<Overlay> listOfOverlays = mapView.getOverlays();
		        listOfOverlays.add(item);
		        listOfOverlays.add(mapOverlay);       
		        
		        
		        mapView.invalidate();*/
		    }

		    public void onStatusChanged(String provider, int status, Bundle extras) {}

		    public void onProviderEnabled(String provider) {}

		    public void onProviderDisabled(String provider) {}

		
		  };

		// Register the listener with the Location Manager to receive location updates
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

		
		 
        /*Geocoder geoCoder = new Geocoder(this, Locale.getDefault());   
            try {
               List<Address> addresses = geoCoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
                if (addresses.size() > 0) {
                    p = new GeoPoint(
                            (int) (addresses.get(0).getLatitude() * 1E6), 
                            (int) (addresses.get(0).getLongitude() * 1E6));
                }   
            } catch (IOException e) {
                e.printStackTrace();
            }
 
        mapController.setZoom(14);
        mapController.animateTo(p);
        
        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);       
 
        mapView.invalidate();*/
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_map, menu);
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private class MyItemizedOverlay extends ItemizedOverlay {
	    private ArrayList< OverlayItem > mOverlays = new ArrayList< OverlayItem >();
	    Context mContext;
	 
	 
	 
	    public MyItemizedOverlay(Drawable marker) {
	        super(boundCenterBottom(marker));
	    }
	 
	    public MyItemizedOverlay(Drawable marker, Context context) {
	        super(boundCenterBottom(marker));
	        mContext = context;
	    }
	 
	    public void addOverlay(OverlayItem overlay) {
	        mOverlays.add(overlay);
	        populate();
	    }
	 
	    @Override
	    protected OverlayItem createItem(int i) {
	        return mOverlays.get(i);
	    }
	 
	    @Override
	    public int size() {
	        return mOverlays.size();
	    }
	 
	    @Override
	    protected boolean onTap(int i) {
	        //when you tap on the marker this will show the informations provided by you when you create in the 
	        //main class the OverlayItem
	       /* OverlayItem item = mOverlays.get(i);
	        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	        dialog.setTitle(item.getTitle());
	        dialog.setMessage(item.getSnippet());
	        dialog.show();*/
	        return true;
	    }
	}
}
